<?php
namespace App\Logic\ZiNiao\Api;

use App\Libs\ZiNiao\RequireAppToken;
use App\Libs\ZiNiao\RequireOpenClient;
use App\Libs\ZiNiao\RPARequest\PlansReplacementRequest;
use Exception;
class PlansReplacement
{
    /**
     * 修改计划并创建任务
     * @param array $bizContent
     * $bizContent = [
        'name' => 'test',
        "platformId" => "0",//亚马逊
        "actionType" => "REPEAT_BY_DAY",
        "repeatTimeAt" => "16:00",
        "validFrom" => null,
        "validTo" => null,
        "scriptList" => [
            [
                "scriptId" => "1002",
                "runWith" => [
                    [
                        "name" => "downloadTimeType",
                        "value" => "last60"
                    ]
                ]
            ]
        ],
     * ];
     * @return mixed
     */
    public function exe($bizContent = [])
    {
        $openClient = RequireOpenClient::getInstance();
        $requireAppToken = new RequireAppToken();
        $appAuthToken = $requireAppToken->getAppToken($openClient);
        $request = new PlansReplacementRequest();
        $request->setBizContent(json_encode($bizContent));
        $result = $openClient->executeAppToken($request,$appAuthToken);// 发送请求
        $result_decode = json_decode($result);
        if (($result_decode->code === 0 or $result_decode->code === '0') and !isset($result_decode->sub_code)) {
            $data = $result_decode->data;
            if ($data->code === 0 or $data->code === '0') {
                return $data->data;
            }else{
                report(new Exception('PlansReplacement失败:' . $data->msg,219));
                return 0;
            }
        }else {
            report(new Exception('PlansReplacement失败:' . $result,219));
            return 0;
        }
    }
}
